﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

namespace GT.DataAccess
{
	public class DataAccess<T1, T2> where T1 : DataContext, new() where T2: DataAccess<T1, T2>
	{
		public T1 DataContext { get; protected set; }
		
		public DataAccess()
		{
			/*DataContext = new T();*/
		}
		[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert)]
		public bool Insert(bool submit)
		{
			DataContext.GetTable<T2>().InsertOnSubmit(this as T2);
			//DataContext.GetTable(this.GetType()).InsertOnSubmit(this);
			if (submit) DataContext.SubmitChanges();
			return true;
		}
		[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert)]
		public bool Insert()
		{
			return Insert(true);
		}
		[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)]
		public bool Update()
		{
			DataContext.SubmitChanges();
			return true;
		}
		[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Delete)]
		public bool Delete(bool submit)
		{
			DataContext.GetTable<T2>().DeleteOnSubmit(this as T2);
			if (submit) DataContext.SubmitChanges();
			return true;
		}
		[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Delete)]
		public bool Delete()
		{
			return Delete(true);
		}
		public IQueryable<T2> Get(int start, int count)
		{
			return DataContext.GetTable<T2>().Skip(start).Take(count);
		}
	}
}